package com.testdocs.client.validation;

import java.util.ArrayList;
import java.util.List;

public class Validator {
	public void validate(SelfValidating selfValidatingObject) throws ConstraintViolationException {
		ValidationConstraint constraints[] = selfValidatingObject.selfValidate();
		if (constraints != null) {
			throw new ConstraintViolationException(constraints);
		}
	}

	public void validate(SelfValidating[] selfValidatingObjects) throws ConstraintViolationException {
		List<ValidationConstraint> constraintsList = new ArrayList<ValidationConstraint>();

		for (SelfValidating selfValidatingObject : selfValidatingObjects) {
			ValidationConstraint[] constraints = selfValidatingObject.selfValidate();
			if (constraints != null) {
				for (ValidationConstraint constraint : constraints) {
					constraintsList.add(constraint);
				}
			}
		}
		if (constraintsList.size() > 0) {
			ValidationConstraint[] result = new ValidationConstraint[constraintsList.size()];
			result = constraintsList.toArray(result);
			throw new ConstraintViolationException(result);
		}
	}
}
